//
//  ForthView.m
//  B段项目
//
//  Created by 昶博 on 15/9/23.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "ForthView.h"
#import "CollectionViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "ForthModel.h"
#import "NextViewController.h"
#import "BelowView.h"
#import "Player.h"
//NSString *Url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.9.5/News/newsgetprograminfo-pm1-pid%ld.json",self.model.program_id];
@interface ForthView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic ,strong)ForthModel *model;

@end
@implementation ForthView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
        flowlayout.minimumInteritemSpacing = 10*w;
        flowlayout.minimumLineSpacing = 10*h;
        flowlayout.itemSize = CGSizeMake((self.frame.size.width/4+20*w), self.frame.size.height/5);
        flowlayout.sectionInset = UIEdgeInsetsMake(10*h, 5*w, 5*h, 5*w);
        self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowlayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.collectionView];
      
   
      
        [self dataHandle];
        
    }
    return self;
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.arr.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    ForthModel*model = [[ForthModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    cell.Model =model;
    
    return cell;
    
}
-(void)dataHandle
{
    self.arr = [NSMutableArray array];
  
    NSString *urlString = @"http://223.99.255.20/app.api.autohome.com.cn/autov4.9.5/News/newsmyradiolist-pm1-tp0.json";

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    //设置安全级别
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //请求成功
        NSDictionary *dic = responseObject;
       
       self.Darr = dic[@"result"][@"list"];
       
        
        for (NSDictionary *dic in self.Darr) {
            if ([dic[@"type"]integerValue]==1) {
                
                [self.arr addObject:dic];
                
            }
        }
        [self.collectionView reloadData];
       
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
    
    
    
}
// 点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    NSDictionary *dic = [self.arr objectAtIndex:indexPath.row];
    ForthModel *model = [[ForthModel alloc]init];
   [model setValuesForKeysWithDictionary:dic];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
   [center postNotificationName:@"push" object:model];
    
      
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
